package com.jindi.appuser.util;

import com.jindi.appuser.common.EmptyUtil;
import com.jindi.appuser.dao.AppMapper;
import com.jindi.appuser.dao.WinningListMapper;
import com.jindi.appuser.domain.App;
import com.jindi.appuser.domain.WinningList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: duanj
 * Date: 2018-03-22
 * Time: 16:06
 * Description:
 */
@Component
public class ScheduledTasks {
    private Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
    @Autowired
    private AppMapper appMapper;
    @Autowired
    private WinningListMapper winningListMapper;

    public static void main(String[] args) {
        //logger.info("==============重置分享标识定时任务开始执行=============");
        int bc = (int) (Math.random() * (10 - 5)) + 5;
        long aa = new Date().getTime();
        long time = new Date().getTime() - 1000 * 60 * bc;
        System.out.println(stampToDate(EmptyUtil.toString(aa)));
        System.out.println(stampToDate(EmptyUtil.toString(time)));
        //logger.info("==============重置分享标识定时任务执行结束=============");
    }


    //重置
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    //重置
    @Scheduled(fixedDelay = 1000 * 60 * 5)
    public void initShare() {
        App app = appMapper.selectByPrimaryKey(1);
        if (new Date().getHours() > 8 && "1".equals(app.getDrowStatus())) {
            int bc = (int) (Math.random() * (10 - 5)) + 5;
            long aa = new Date().getTime();
            long time = new Date().getTime() - 1000 * 60 * bc;
            System.out.println(time);
            WinningList winningList = winningListMapper.getRand();
            long lt = new Long(time);
            Date date = new Date(lt);
            winningList.setCreateTime(date);
            winningListMapper.updateByPrimaryKeySelective(winningList);
        }
    }

    //重置
    @Scheduled(fixedDelay = 1000 * 60 * 10)
    public void initShare1() {
        App app = appMapper.selectByPrimaryKey(1);
        if (new Date().getHours() < 8 && "1".equals(app.getDrowStatus())) {
            int bc = (int) (Math.random() * (10 - 5)) + 5;
            long aa = new Date().getTime();
            long time = new Date().getTime() - 1000 * 60 * bc;
            System.out.println(time);
            WinningList winningList = winningListMapper.getRand();
            long lt = new Long(time);
            Date date = new Date(lt);
            winningList.setCreateTime(date);
            winningListMapper.updateByPrimaryKeySelective(winningList);
        }
    }
    /*
     * 将时间戳转换为时间
     */

}